﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace ykglxt
{
    public partial class Form_YPBM : Form
    {
        public Form_YPBM()
        {
            InitializeComponent();
        }

        int firsttime = 0;
        bool empty = true;
        DataSet ds = new DataSet();

        private void Form_YPBM_Load(object sender, EventArgs e)
        {
            this.dic_YPTableAdapter.Fill(this.yKGLXTDataSet.Dic_YP);
            dgvYP.DataSource = yKGLXTDataSet.Dic_YP.DefaultView;
            yKGLXTDataSet.Dic_YP.DefaultView.RowFilter = "LX=0";
        }

        private void bindingNavigatorMovePreviousItem_Click(object sender, EventArgs e)
        {
            int rowindx = (dgvYP.SelectedRows[0].Index - 1 + dgvYP.RowCount) % dgvYP.RowCount;
            dgvYP.ClearSelection();
            dgvYP.Rows[rowindx].Selected = true;
            dgvYP.FirstDisplayedScrollingRowIndex = dgvYP.SelectedRows[0].Index;
        }

        private void bindingNavigatorMoveNextItem_Click(object sender, EventArgs e)
        {
            int rowIndex = (dgvYP.SelectedRows[0].Index + 1) % dgvYP.RowCount;
            dgvYP.ClearSelection();
            dgvYP.Rows[rowIndex].Selected = true;
            dgvYP.FirstDisplayedScrollingRowIndex = rowIndex;
            if (rowIndex < dgvYP.FirstDisplayedScrollingRowIndex)
            {
                dgvYP.FirstDisplayedScrollingRowIndex = rowIndex;
            }
            else if (!dgvYP.Rows[rowIndex].Displayed)
            {
                dgvYP.FirstDisplayedScrollingRowIndex++;
            }

        }

        private void bindingNavigatorMoveFirstItem_Click(object sender, EventArgs e)
        {
            dgvYP.ClearSelection();
            dgvYP.Rows[0].Selected = true;
            dgvYP.FirstDisplayedScrollingRowIndex = 0;
        }

        private void bindingNavigatorMoveLastItem_Click(object sender, EventArgs e)
        {
            dgvYP.ClearSelection();
            dgvYP.Rows[dgvYP.RowCount - 2].Selected = true;
            dgvYP.FirstDisplayedScrollingRowIndex = dgvYP.RowCount - 1;

        }

        private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
        {
            dgvYP.ClearSelection();
            dgvYP.Rows[dgvYP.NewRowIndex].Selected = true;
            dgvYP.FirstDisplayedScrollingRowIndex = dgvYP.NewRowIndex;
            dgvYP.CurrentCell = dgvYP.Rows[dgvYP.NewRowIndex].Cells[1];
            Form_YPBM_Set YPBM_Set = new Form_YPBM_Set(dgvYP.Rows[dgvYP.CurrentCell.RowIndex]);
            if (YPBM_Set.ShowDialog() == DialogResult.OK)
            {
                if(rdbXY.Checked)
                    dgvYP.CurrentRow.Cells[0].Value = "0";
                if (rdbZCY.Checked)
                    dgvYP.CurrentRow.Cells[0].Value = "3";
                if (rdbCY.Checked)
                    dgvYP.CurrentRow.Cells[0].Value = "4";
            }
        }

        private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确认删除该记录？", "请选择", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                dgvYP.Rows.RemoveAt(this.dgvYP.CurrentRow.Index);
            }
        }

        private void tsbSave_Click(object sender, EventArgs e)
        {
            dgvYP.CurrentCell = null;
            try
            {
                MessageBox.Show("保存成功，已保存" + dic_YPTableAdapter.Adapter.Update(yKGLXTDataSet.Dic_YP).ToString() + "条修改记录！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (SqlException ex)
            {
                if (ex.Number == 2627)
                    MessageBox.Show("保存失败，编码必须是唯一的，请查看是否存在重复的编码！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                else if (ex.Number == 515)
                    MessageBox.Show("保存失败，编码不能为空，请查看是否存在记录缺填编码！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                else
                    MessageBox.Show("保存失败，网络故障，请稍后再试！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void rdbXY_CheckedChanged(object sender, EventArgs e)
        {
            yKGLXTDataSet.Tables["Dic_YP"].DefaultView.RowFilter = "LX=0";
        }

        private void rdbZCY_CheckedChanged(object sender, EventArgs e)
        {
            yKGLXTDataSet.Tables["Dic_YP"].DefaultView.RowFilter = "LX=3";
        }

        private void rdbCY_CheckedChanged(object sender, EventArgs e)
        {
            yKGLXTDataSet.Tables["Dic_YP"].DefaultView.RowFilter = "LX=4";
        }

        private void dgvYP_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            Form_YPBM_Set YPBM_Set = new Form_YPBM_Set(dgvYP.CurrentRow);
            YPBM_Set.ShowDialog();
        }

        private void dgvYPDW_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.tbxYPDW.Text = this.dgvYPDW.CurrentRow.Cells[0].Value.ToString();
                this.dgvYPDW.Visible = false;
                try
                {
                    for (int i = 0; i < this.dgvYP.Rows.Count; i++)
                    {
                        if (tbxYPDW.Text == this.dgvYP[this.dgvYP.Columns[1].Index, this.dgvYP.Rows[i].Index].Value.ToString())
                        {
                            this.dgvYP.ClearSelection();
                            this.dgvYP.Rows[i].Selected = true;
                            this.dgvYP.CurrentCell = this.dgvYP.Rows[i].Cells[1];
                            return;
                        }
                    }
                }
                catch (NullReferenceException)
                {
                    return;
                }
            }

        }

        private void tbxYPDW_KeyDown(object sender, KeyEventArgs e)
        {
            if (empty)
                return;

            if (e.KeyCode == Keys.Down)
            {
                dgvYPDW.Focus();
                if (dgvYPDW.CurrentRow.Index + 1 < dgvYPDW.Rows.Count)
                {
                    dgvYPDW.CurrentCell = dgvYPDW[0, dgvYPDW.CurrentRow.Index + 1];
                }
            }

            if (e.KeyCode == Keys.Enter)
            {
                this.tbxYPDW.Text = this.dgvYPDW.CurrentRow.Cells[0].Value.ToString();
                this.dgvYPDW.Visible = false;
            }
        }


        private bool getData()
        {
            bool res;
            try
            {
                res = false;

                using (SqlConnection conn = new SqlConnection(PublicUnit.ConnetionString))
                {
                    try
                    {
                        conn.Open();
                        SqlDataAdapter da = new SqlDataAdapter("select bm as '编码',zjf as '助记码',mc AS '药品名称',gg AS '规格',sccs AS '生产产家'  from dic_yp", conn);
                        ds = new DataSet();
                        da.Fill(ds, "YPDW");
                        this.dgvYPDW.DataSource = ds.Tables["YPDW"].DefaultView;
                        res = false;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("error!" + ex.ToString());
                        res = true;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("error!" + ex.ToString());
                res = true;
            }
            return res;
        }

        private void tbxYPDW_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (tbxYPDW.ReadOnly) // 只读, 不处理 
                return;

            if ((int)e.KeyChar <= 32) // 特殊键(含空格), 不处理 
                return;

            if (!char.IsDigit(e.KeyChar) && !char.IsLetter(e.KeyChar)) // 非数字字母键, 放弃该输入 
            {
                e.Handled = true;
                return;
            }
        }

        private void dgvYPDW_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            this.tbxYPDW.Text = this.dgvYPDW.CurrentRow.Cells[0].Value.ToString();
            this.dgvYPDW.Visible = false;
            try
            {
                for (int i = 0; i < this.dgvYP.Rows.Count; i++)
                {
                    if (tbxYPDW.Text == this.dgvYP[this.dgvYP.Columns[1].Index, this.dgvYP.Rows[i].Index].Value.ToString())
                    {
                        this.dgvYP.ClearSelection();
                        this.dgvYP.Rows[i].Selected = true;
                        this.dgvYP.CurrentCell = this.dgvYP.Rows[i].Cells[1];
                        return;
                    }
                }
            }catch(NullReferenceException)
            {
                return;
            }
        }

        private void tbxYPDW_TextChanged(object sender, EventArgs e)
        {
            try
            {
                this.dgvYPDW.Top = this.tbxYPDW.Top + this.tbxYPDW.Height;

                if (firsttime == 0)
                {
                    firsttime = 1;
                    empty = getData();
                }

                if (empty)
                    return;

                if (tbxYPDW.Text == " ")
                {
                    if (ds.Tables["YPDW"].DefaultView.Count != 0)
                        dgvYPDW.Visible = true;

                }
                else
                {
                    string dh = "编码 like '" + tbxYPDW.Text.Trim() + "%' or 药品名称 like '%" + tbxYPDW.Text.Trim() + "%'" + " or 助记码 like '" + tbxYPDW.Text.Trim() + "%'";
                    ds.Tables["YPDW"].DefaultView.RowFilter = dh.ToString();
                }
                if (ds.Tables["YPDW"].DefaultView.Count != 0)
                {
                    dgvYPDW.Visible = true;
                    dgvYPDW.CurrentCell = dgvYPDW[0, 0];
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void tsbEdit_Click(object sender, EventArgs e)
        {
            Form_YPBM_Set YPBM_Set = new Form_YPBM_Set(dgvYP.CurrentRow);
            YPBM_Set.ShowDialog();
        }

    }
}
